<!doctype html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>axios基本使用</title>
    <link crossorigin="anonymous" href="https://cdn.bootcss.com/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
</head>

<body>
    <div class="container">
        <h2 class="page-header">基本使用</h2>
        <button class="btn btn-primary" onclick="sendGET()"> 发送GET请求 </button>
        <button class="btn btn-warning" onclick="sendPOST()"> 发送POST请求 </button>
        <button class="btn btn-success" onclick="sendPut()"> 发送 PUT 请求 </button>
        <button class="btn btn-danger" onclick="sendDELETE()"> 发送 DELETE 请求 </button>
    </div>
    <script>
        // react 和 vue 框架
        function sendGET(){
            //发送 AJAX 请求
            axios({
                //请求的类型
                method: "GET",
                //请求 URL
                url: 'http://127.0.0.1/serve',
                //设置请求头 
                headers: {
                    a:100,
                    b:200
                }
            })
            .then(response => {
                console.log(response);
            })
            .catch(error => {
                console.log(error);
            })
        }
    
        function sendPOST(){
            axios({
                //请求类型
                method:'POST',
                //url参数
                params: {
                    wd: 'abc',
                    keyword: 'def'
                },
                //url
                url: 'http://127.0.0.1/server',// wd  keyword
                //请求头
                headers:{
                    c: 300,
                    d: 400
                },
                //请求体
                // data: 'e=500&f=600'
                data: {
                    x: 4000,
                    y: 5000
                }
            }).then(response => {
                console.log(response);
            })
        }
    
        function sendPut(){
            axios({
                method:"PUT",
                url: 'http://127.0.0.1/server'
            }).then(response => {
                console.log(response);
            })
        }

        function sendDELETE(){
            axios({
                method:"delete",
                url: 'http://127.0.0.1/server'
            }).then(response => {
                console.log(response);
            })
        }
    </script>
</body>
</html>